package com.r_z.mapper;

import com.r_z.entity.Storages;
import com.r_z.entity.StoragesDetails;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface StoragesMapper {
    /**
     * 分页查询入库
     *
     * @param page  当前页
     * @param limit 显示数
     * @param where 条件
     * @return List<Storages> 集合对象
     */
    @Select("SELECT p.purchaseId AS purchaseId,\n" +
            "p.total AS total,\n" +
            "u.userName AS userName,\n" +
            "p.nowTime AS nowTime,\n" +
            "p.storageinState AS storageinState,\n" +
            "(SELECT goodName FROM goods WHERE goodId=pd.`goodId`) AS goodName,\n" +
            "pd.`goodId` AS goodId\n" +
            "FROM purchase p \n" +
            "JOIN users u ON p.userId=u.userId \n" +
            "JOIN purchaseDetails pd ON p.`purchaseId`=pd.`purchaseId`\n" +
            "WHERE 1=1\n" +
            "${where} LIMIT #{page},#{limit}")
    List<Storages> selectStorages(@Param("page") int page, @Param("limit") int limit, @Param("where") String where);

    /**
     * 查询采购详情
     *
     * @param purchaseId 采购id
     * @return 详情数据
     */
    @Select("SELECT pd.price,pd.quantity ,pd.goodId,g.goodName,b.`brandName`" +
            "FROM purchaseDetails pd JOIN goods g ON pd.goodId=g.goodId JOIN brand b ON g.brandId=b.brandId WHERE purchaseId=#{purchaseId}")
    StoragesDetails selectDetail(@Param("purchaseId") String purchaseId);

    /**
     * 查询条数
     *
     * @param where 条件
     * @return 条数
     */
    @Select("select count(*) from purchase p join users u on " +
            "p.userId=u.userId where 1=1  ${where}")
    int getTotal(@Param("where") String where);

    @Update("update purchase set storageinState=1 where purchaseId=#{purchaseId}")
    int approve(@Param("purchaseId") String purchaseId);

    @Select("select count(*) from repertory where productId=#{goodId}")
    int repertoryCount(@Param("goodId") int goodId);

    @Update("update repertory set quantity=quantity+ #{num} where productId=#{goodId}")
    void repertoryInsert(@Param("num") int num, @Param("goodId") int goodId);

    @Insert("INSERT INTO repertory(productId,quantity) VALUES(#{goodId},#{num})")
    void repertoryAdd(@Param("num") int num, @Param("goodId") int goodId);
}
